package com.youdoneed.community.forum.service;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.dao.ForumallowviewrecordMapper;
import com.youdoneed.community.forum.entity.Forumallowviewrecord;

/**
 * 帖子可见记录服务接口
 *
 * @author lid
 * @date 2017.6.13
 */
@Service
public class ForumallowviewrecordService {

    private final Logger logger = LoggerFactory.getLogger(ForumallowviewrecordService.class);

    @Autowired
    ForumallowviewrecordMapper forumallowviewrecordMapper;

    @Transactional
    public int saveForumallowviewrecord(Forumallowviewrecord forumallowviewrecord) {
        logger.info("saveForumallowviewrecord!");
        if (null == forumallowviewrecord) {
            logger.info("forumallowviewrecord参数为空!");
            throw new AppException("forumallowviewrecord参数不能为空");
        }
        try {
            forumallowviewrecord.setForumAllowViewRecordId(StringUUIdUtil.getUUID());
            forumallowviewrecord.setCreateDate(new Date());
            return forumallowviewrecordMapper.insert(forumallowviewrecord);
        } catch (Exception e) {
            logger.info("保存帖子可见记录失败！" + e);
            return 0;
        }
    }

    @Transactional
    public int deleteForumallowviewrecord(String forumAllowViewRecordId) {
        logger.info("forumAllowViewRecordId!");
        if (!StringUtils.hasText(forumAllowViewRecordId)) {
            logger.info("forumAllowViewRecordId参数为空!");
            throw new AppException("forumAllowViewRecordId参数不能为空");
        }
        try {
            return forumallowviewrecordMapper.deleteByPrimaryKey(forumAllowViewRecordId);
        } catch (Exception e) {
            logger.info("删除帖子可见记录失败！" + e);
            return 0;
        }
    }

    @Transactional
    public List<Forumallowviewrecord> selectByMsgId(String msgId) {
        if (!StringUtils.hasText(msgId)) {
            throw new AppException("msgId参数为空");
        }

        try {
            return forumallowviewrecordMapper.selectByMsgId(msgId);
        } catch (Exception e) {
            logger.info("获取帖子可见记录失败！" + e);
            return null;
        }
    }

    @Transactional
    public int deleteByMsgId(String msgId) {
        if (!StringUtils.hasText(msgId)) {
            throw new AppException("msgId参数为空");
        }

        try {
            return forumallowviewrecordMapper.deleteByMsgId(msgId);
        } catch (Exception e) {
            logger.info("删除帖子可见记录失败！" + e);
            return -1;
        }
    }
}
